const { ccclass, property } = cc._decorator;


@ccclass
export class EventBase extends cc.Component {

    @property({
        displayName: '注册触摸事件',
        tooltip: '是否从创建阶段就自动注册触摸事件',
    })
    isAutoRegisterEvent: boolean = false;
    @property({
        displayName: '按钮效果'
    })
    buttonEffect: boolean = false;
    private buttonEffectScale = 1.1;
    isRegisteredEvent: boolean = false;
    dt: number = 0;
    touch: cc.Event.EventTouch = null;
    // 全局点击总个数
    private static _touchCount: number = 0;
    set touchCount(v) {
        EventBase._touchCount = v;
        // console.log('touchCount', EventBase._touchCount)
    }
    get touchCount() {
        return EventBase._touchCount;
    }
    private touchScaleTween: cc.Tween = null;
    isOnLoadStage: boolean = true;
    onLoad() {
        if (this.isAutoRegisterEvent) {
            this.registerEvent();
        }
        this.scheduleOnce(() => {
            this.isOnLoadStage = false;
        }, 0)
    }

    protected touchStart(touch: cc.Event.EventTouch) {
        this.touch = touch;
        this.touchCount++;
        if (this.buttonEffect) {
            cc.tween(this.node)
                .to(0.1, { scale: this.node.scaleX > 0 ? this.buttonEffectScale : -this.buttonEffectScale })
                .start();
        }
        // touch.stopPropagation();
    }
    protected touchMove(touch: cc.Event.EventTouch) {
        // touch.stopPropagation();
    }
    protected touchEnd(touch: cc.Event.EventTouch) {
        if (this.buttonEffect) {
            this.resetScale();
        }
        this.touchOver(touch);
        // touch.stopPropagation();
    }
    protected touchCancel(touch: cc.Event.EventTouch) {
        if (this.buttonEffect) {
            this.resetScale();
        }
        this.touchOver(touch);
        // touch.stopPropagation();
    }
    protected touchOver(touch: cc.Event.EventTouch) {
        this.resetTouch();
    }

    protected resetTouch() {
        this.touchCount = 0;
        this.touch = null;
    }

    resetScale() {
        console.log('resetScale', this.node.name)
        cc.tween(this.node)
            .to(0.1, { scale: this.node.scaleY > 0 ? 1 : -1 })
            .start();
    }

    // onDestroy() {
    //     if (this.node && this.node.isValid) {
    //         console.log('销毁了', this.node.name)
    //         this.node.emit(CustomEvent.onDestroy)
    //     }
    // }

    public registerEvent(node: cc.Node = null) {
        node = node || this.node;
        node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this)
        this.isRegisteredEvent = true;
    }
    public cancelEvent(node: cc.Node = null) {
        node = node || this.node;
        node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this)
        this.isRegisteredEvent = false;
    }
    protected update(dt: number) {
        this.dt = dt;
    }
}
